from redis import Redis
import threading
# from Singleton import *
#
# @singleton
class REDIS(object):
    __lock = threading.RLock()
    def __init__(self, host, port,password,db):
        self.__host = host
        self.__port = port
        self.__password = password
        self.__db = db

        try:
            self.__redis = Redis(host=host, port=self.__port, password=self.__password,db=self.__db)
        except Exception as e:
            print ("init error: %s" % e)
    def locks(func):
        """
        :param func: 被装饰的函数
        :return: 对每个查询进行加锁
        """
        # print(func)
        def wrapper(self,*args, **kwargs):
            """
            :param args:收集被装饰函数的参数
            :param kwargs:收集被装饰函数的关键字参数
            :return:
            """
            try:
                REDIS.__lock.acquire()
                result = func(self,*args, **kwargs)
            except Exception as e:
                return e
            finally:
                REDIS.__lock.release()
            return result

        return wrapper
    @locks
    def add(self, skey,sval):
        result = self.__redis.sadd(skey,sval)
        return result

    # def sget(self, skey):
    #     return self.__redis.sget(skey)
    @locks
    def count(self, skey):
        count=self.__redis.scard(skey)
        return count

    @locks
    def members(self, skey):
        list=self.__redis.smembers(skey)
        return list

    @locks
    def sdelete(self, skey):
        result = self.__redis.spop(skey)
        return result

    @locks
    def set(self, skey,sval):
        return self.__redis.set(skey,sval)

    @locks
    def get(self, skey):
        return self._ _redis.get(skey)

    @locks
    def delete(self, skey):
        return self.__redis.delete(skey)

    @locks
    def sremove(self,skey,object):
        return self.__redis.srem(skey,object)

    @locks
    def expire(self, skey,ex):
        return self.__redis.expire(skey,ex)

    @locks
    def set_lpush(self,key,list):
        '''lpush/rpush -- 从左/右插入数据'''
        rest = self.__redis.lpush(key,list)
        return rest

    @locks
    def set_rpush(self,key,list):
        '''lpush/rpush -- 从左/右插入数据'''
        rest = self.__redis.rpush(key,list)
        return rest

    @locks
    def set_lpop(self,key):
        '''lpush/rpush -- 从左/右移除数据返回数据'''
        rest = self.__redis.lpop(key)
        return rest

    @locks
    def set_rpop(self,key):
        '''lpush/rpush -- 从左/右移除数据返回数据'''
        rest = self.__redis.rpop(key)
        return rest

    @locks
    def get_push(self,key,start,end):
        res = self.__redis.lrange(key,start,end)
        return res

# rds = REDIS("172.16.104.20", 6379,0)
rds = REDIS(host="192.168.245.4", port='6379',password='',db=11)

if __name__ == "__main__":
#     rds = REDIS("123.59.185.66", 6379,1)
    rds.add("aa",1)
    rds.add("aa",2)
    rds.add("aa",3)
    rds.add("aw",3)
    print ("count:",rds.count("aa"))
    print ("count:",rds.count("aw"))
    print ("member:",rds.members("aa"))
    print ("member:",rds.members("aw"))
    print ("delete:",rds.delete("aa"))
    print ("member:",rds.members("aa"))







